package com.alesj.newsfeed.web.form;

import java.util.List;
import java.util.Set;
import java.io.File;

import com.alesj.newsfeed.file.ByteAdapter;
import com.alesj.newsfeed.file.AttachmentHandler;
import com.alesj.newsfeed.data.Message;
import com.alesj.newsfeed.data.Account;
import com.generalynx.common.mail.ByteArrayDataSource;
import com.generalynx.ecos.connector.mail.client.IMailClient;
import com.generalynx.ecos.data.dao.IBasicDAO;
import com.generalynx.ecos.data.dao.IBasicDAOAware;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.servlet.mvc.SimpleFormController;

public abstract class ExecutorHolderController extends SimpleFormController implements IBasicDAOAware {

    protected AttachmentHandler<File> attachmentHandler;
    protected TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
    protected IMailClient mailClient;
    protected IBasicDAO basicDAO;
    protected String encoding = "UTF-8";

    protected void addAttachments(MimeMessageHelper helper, List<? extends ByteAdapter> attachments)
            throws Exception {
        if (attachments != null) {
            for(ByteAdapter fub : attachments) {
                helper.addAttachment(fub.getName(), new ByteArrayDataSource(fub.getBytes(), null));
            }
        }
    }

    protected void addAttachments(MimeMessageHelper helper, File[] attachments)
            throws Exception {
        if (attachments != null) {
            for(File fub : attachments) {
                helper.addAttachment(fub.getName(), fub);
            }
        }
    }

    protected void sendReport(Message message)
    {
        try {
            logger.info("Sending report message '" + message + "' to " + message.getCreator().getPerson());

            Account account = message.getAccount();
            MimeMessageHelper helper = new MimeMessageHelper(getMailClient().createMimeMessage(), false, encoding);
            helper.setFrom(account.getEmail(), account.getName());
            helper.setTo(message.getCreator().getPerson().getEmail());
            helper.setSubject(message.getSubject() + " - Report");

            StringBuilder buffer = new StringBuilder();
            buffer.append("Recepients: ").append("\n");
            Set recepients = message.getRecepients();
            for (Object recepient : recepients)
                buffer.append(recepient).append("\n");
            buffer.append("-------------------------\n");
            buffer.append("Failed: ").append("\n");
            Set failed = message.getFailed();
            for (Object fail : failed)
                buffer.append(fail).append("\n");

            helper.setText(buffer.toString(), false);
            getMailClient().sendMail(helper.getMimeMessage());
        }
        catch (Throwable t) {
            logger.warn("Cannot send report for msg: " + message, t);
        }
    }

    public AttachmentHandler<File> getAttachmentHandler() {
        return attachmentHandler;
    }

    public void setAttachmentHandler(AttachmentHandler<File> attachmentHandler) {
        this.attachmentHandler = attachmentHandler;
    }

    public TaskExecutor getTaskExecutor() {
        return taskExecutor;
    }

    public void setTaskExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    public IMailClient getMailClient() {
        return mailClient;
    }

    public void setMailClient(IMailClient mailClient) {
        this.mailClient = mailClient;
    }

    public IBasicDAO getBasicDAO() {
        return basicDAO;
    }

    public void setBasicDAO(IBasicDAO basicDAO) {
        this.basicDAO = basicDAO;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
}
